package org.tom.org.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.tom.basic.query.BaseQuery;
import org.tom.basic.service.impl.BaseServiceImpl;
import org.tom.basic.util.PageList;
import org.tom.org.domain.Department;
import org.tom.org.mapper.DepartmentMapper;
import org.tom.org.query.DepartmentQuery;
import org.tom.org.service.IDepartmentService;

import java.util.List;
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    @Transactional
    public void update(Department department) {
        if (department.getParent()==null){ // 判断是否为一级部门（没有父部门）如果没有父部门
            department.setDirPath("/"+department.getId()); // dirpath就是/自己的id
        }else { // 有父部门
            if (department.getParent().getId()!=null){ // 判断修改的时候修改是否是顶级部门，由于修改的时候parment肯定不为空但是修改为一级部门的时候parment传过来的值为parment{}
                Department parentDepartment = departmentMapper.loadById(department.getParent().getId());
                department.setDirPath(parentDepartment.getDirPath()+"/"+department.getId()); // dirpath就是父部门的dirpath+/自己的id
            } else { // 修改为一级部门直接dirpath就是/自己的id
                department.setDirPath("/"+department.getId());
            }
        }
        departmentMapper.update(department);
    }



    @Override
    @Transactional
    public void insert(Department department) {
        departmentMapper.add(department);
        // 调用修改方法
        update(department);
    }





    /* 查询部门树，也就是从没有上级部门的 */
    @Override
    public List<Department> loadTree() {
        return departmentMapper.loadTree();
    }


}
